package com.djhu.freepartner.web.service.check;

import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.core.constant.Global;
import com.djhu.freepartner.web.control.manager.SessionManager;
import com.djhu.freepartner.web.entity.enums.Role;
import com.djhu.freepartner.web.support.HttpKit;

import javax.servlet.http.HttpServletRequest;

public class CheckService implements ICheck{


    @Override
    public boolean check(Object[] permissions) {
        HttpServletRequest servletRequest = HttpKit.getRequest();
        String sessionid = servletRequest.getHeader(Global.SESSION_KEY);
        PartnerUser user = SessionManager.me().selectUserBySessionId(sessionid);
        String role = user.getRole();
        Role userRole = Role.valueOf(role);

        for (Object permission:permissions){
            String p = String.valueOf(permission);
            Role methodRole = Role.valueOf(p);
            if (userRole.getRoleCode() >= methodRole.getRoleCode()){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean checkAll() {
        return true;
    }
}
